package com.cc2019.buke.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.viewpager.widget.ViewPager;

import com.cc2019.buke.R;
import com.cc2019.buke.find.MainActivity;
import com.cc2019.buke.home.activity.ProActivity;
import com.cc2019.buke.home.activity.ProChannelActivity;
import com.cc2019.buke.home.activity.ProRxActivity;
import com.cc2019.buke.home.activity.ProinfoActivity;
import com.cc2019.buke.home.activity.PxActivity;
import com.cc2019.buke.home.entry.HomeDataBean;
import com.cc2019.buke.home.entry.Product;
import com.cc2019.buke.home.entry.Product3;
import com.cc2019.buke.home.entry.Product4;
import com.cc2019.buke.home.entry.Product5;
import com.cc2019.buke.home.entry.Product7;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.DefaultSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.zhy.magicviewpager.transformer.RotateDownPageTransformer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter {
    public static final int PLAYER = 0;
    public static final int BANNER = 1;
    public static final int CHANNEL = 2;
    public static final int AD = 3;
    public static final int SG = 4;
    public static final int CX = 5;
    public static final int TJ = 6;

    public static final int QG = 7;
    public static final int SC = 8;
    public static final int RX = 9;

    private Context context;
    private HomeDataBean homeDataBean;
    RxAdapter rxAdapter;

    public HomeAdapter(Context context, HomeDataBean homeDataBean){
        this.context = context;
        this.homeDataBean = homeDataBean;
    }


    @NonNull
    @Override //创建viewHolder
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case PLAYER:
                View view9 = LayoutInflater.from(context).inflate(R.layout.player_layout,parent,false);
                holder = new PlayerHolder(view9);
                break;
            case BANNER:
                View view1 = LayoutInflater.from(context).inflate(R.layout.home_banner_item,parent,false);
                holder = new BannerHolder(view1);
                break;
            case CHANNEL:
                View view2 = LayoutInflater.from(context).inflate(R.layout.home_channel_item,parent,false);
                holder = new ChannelHolder(view2);
                break;
            case AD:
                View view3 = LayoutInflater.from(context).inflate(R.layout.home_ad_item,parent,false);
                holder = new AdHolder(view3);
                break;
            case SG:
                View view4 = LayoutInflater.from(context).inflate(R.layout.home_sg_layout,parent,false);
                holder = new SgHolder(view4);
                break;
            case CX:
                View view5 = LayoutInflater.from(context).inflate(R.layout.home_cx_item,parent,false);
                holder = new CxHolder(view5);
                break;
            case TJ:
                View view6 = LayoutInflater.from(context).inflate(R.layout.home_tj_layout,parent,false);
                holder = new TjHolder(view6);
                break;
            case QG:
                View view7 = LayoutInflater.from(context).inflate(R.layout.home_qg_item,parent,false);
                holder = new QgHolder(view7);
                break;
            case SC:
                View view8 = LayoutInflater.from(context).inflate(R.layout.home_sc_item,parent,false);
                holder = new ScHolder(view8);
                break;
            case RX:
                View view10 = LayoutInflater.from(context).inflate(R.layout.home_rx_layout,parent,false);
                holder = new RxHolder(view10);
                break;

        }
        return holder;
    }

    @Override//绑定数据
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int type =  getItemViewType(position);
        switch (type){

        }

    }

    @Override
    public int getItemViewType(int position) {
        int type = 0;
        switch (position){
            case PLAYER:
                type = PLAYER;
                break;
            case BANNER:
                type = BANNER;
                break;
            case CHANNEL:
                type = CHANNEL;
                break;
            case AD:
                type = AD;
                break;
            case SG:
                type = SG;
                break;
            case CX:
                type = CX;
                break;
            case TJ:
                type = TJ;
                break;
            case RX:
                type = RX;
                break;
            case QG:
                type = QG;
                break;
            case SC:
                type = SC;
                break;
        }
        return type;
    }


    @Override//总共有几项
    public int getItemCount() {
        return 10;
    }

    class PlayerHolder extends RecyclerView.ViewHolder{
        Button button;
        public PlayerHolder(@NonNull View itemView) {
            super(itemView);
            button =itemView.findViewById(R.id.play);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, MainActivity.class);
                    context.startActivity(intent);
                }
            });
        }

    }
    class BannerHolder extends RecyclerView.ViewHolder{
        SliderLayout sliderShow;

        public BannerHolder(@NonNull View itemView) {
            super(itemView);
            sliderShow = (SliderLayout)itemView.findViewById(R.id.slider);

            DefaultSliderView textSlider = new DefaultSliderView(context);
            textSlider.description("小王子").image(R.drawable.b1);
            sliderShow.addSlider(textSlider);

//            textSlider.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
//                @Override
//                public void onSliderClick(BaseSliderView slider) {
//                    sliderShow.stopAutoCycle();
//                    new Handler().postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            sliderShow.startAutoCycle();
//                        }
//                    },2000);
//                }
//            });
            TextSliderView textSliderView2 = new TextSliderView(context);
            textSliderView2.description("雾都孤儿").image(R.drawable.b2);
            sliderShow.addSlider(textSliderView2);

            TextSliderView textSliderView3 = new TextSliderView(context);
            textSliderView3.description("爱丽丝梦游仙境").image(R.drawable.b3);
            sliderShow.addSlider(textSliderView3);

            sliderShow.setDuration(2000);
            sliderShow.setPresetTransformer(SliderLayout.Transformer.FlipPage);
            sliderShow.setPresetIndicator(SliderLayout.PresetIndicators.Right_Bottom);
            sliderShow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context,ProChannelActivity.class);
                    context.startActivity(intent);
                }
            });
        }
    }
    class ChannelHolder extends RecyclerView.ViewHolder{
        List<Product> pro_list1;
        RecyclerView recyclerView1;
        public ChannelHolder(@NonNull View itemView) {
            super(itemView);
            recyclerView1 = (RecyclerView)itemView.findViewById(R.id.ch_recycleView);
            pro_list1 = init();
            ChannelAdapter channelAdapter = new ChannelAdapter(context,pro_list1);
            recyclerView1.setAdapter(channelAdapter);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context,5);
            recyclerView1.setLayoutManager(gridLayoutManager);
            //设置监听事件
            channelAdapter.setOnCommProClick(new ChannelAdapter.OnCommProClick() {
                @Override
                public void onItemClick(Product product) {
                    Intent intent = new Intent(context, ProChannelActivity.class);
                    intent.putExtra("product2",product);
                    context.startActivity(intent);
                }
            });

        }
        public List<Product> init(){
            ArrayList<Product> pros = new ArrayList<Product>();
            Product product1 = new Product("言情",R.drawable.a1);
            Product product2 = new Product("童话",R.drawable.a2);
            Product product3 = new Product("古风",R.drawable.a3);
            Product product4 = new Product("名著",R.drawable.a4);
            Product product5 = new Product("经典",R.drawable.a5);
            Product product6 = new Product("玄幻",R.drawable.a6);
            Product product7 = new Product("古风",R.drawable.a7);
            Product product8 = new Product("青春",R.drawable.a8);
            Product product9 = new Product("恐怖",R.drawable.a9);
            Product product10 = new Product("更多",R.drawable.a10);
            pros.add(product1);
            pros.add(product2);
            pros.add(product3);
            pros.add(product4);
            pros.add(product5);pros.add(product6);pros.add(product7);pros.add(product8);pros.add(product9);pros.add(product10);
            return pros;

        }
    }
    class ScHolder extends RecyclerView.ViewHolder{
        List<Product5> pro_list1;
        RecyclerView recyclerView1;
        public ScHolder(@NonNull View itemView) {
            super(itemView);
            recyclerView1 = (RecyclerView)itemView.findViewById(R.id.sc_recycleView);
            pro_list1 = init();
            ScAdapter scAdapter = new ScAdapter(context,pro_list1);
            recyclerView1.setAdapter(scAdapter);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context,4);
            recyclerView1.setLayoutManager(gridLayoutManager);
            //设置监听事件
            scAdapter.setOnCommProClick(new ScAdapter.OnCommProClick() {
                @Override
                public void onItemClick(Product5 product) {
                    Intent intent = new Intent(context, ProChannelActivity.class);
                    intent.putExtra("product2",product);
                    context.startActivity(intent);
                }
            });

        }
        public List<Product5> init(){
            ArrayList<Product5> pros = new ArrayList<Product5>();
            Product5 product1 = new Product5("书香童年","礼物季来了",R.drawable.sct1);
            Product5 product2 = new Product5("品质好书","2022主编推荐",R.drawable.sct2);
            Product5 product3 = new Product5("阅读童年","2022重磅推荐",R.drawable.sct3);
            Product5 product4 = new Product5("身体认识","了解自己",R.drawable.sct4);
            Product5 product5 = new Product5("畅读经典","每满100减50",R.drawable.sct5);
            Product5 product6 = new Product5("历史好书","每满100减50",R.drawable.sct6);
            Product5 product7 = new Product5("哲学宗教","每满100减50",R.drawable.sct7);
            Product5 product8 = new Product5("文学艺术","每满100减50",R.drawable.sct8);
            pros.add(product1);
            pros.add(product2);
            pros.add(product3);
            pros.add(product4);
            pros.add(product5);pros.add(product6);pros.add(product7);pros.add(product8);
            return pros;

        }
    }

    class CxHolder extends RecyclerView.ViewHolder{
        List<Product> pro_list2;
        RecyclerView recyclerView2;
        public CxHolder(@NonNull View itemView) {
            super(itemView);
            recyclerView2 = (RecyclerView)itemView.findViewById(R.id.cx_recycleView);
            pro_list2 = init();
            CxAdapter cxAdapter = new CxAdapter(context,pro_list2);
            recyclerView2.setAdapter(cxAdapter);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context,4);
            recyclerView2.setLayoutManager(gridLayoutManager);
            cxAdapter.setOnCommProClick(new CxAdapter.OnCommProClick() {
                @Override
                public void onItemClick(Product product) {
                    Intent intent = new Intent(context, PxActivity.class);
                    intent.putExtra("product",product);
                    context.startActivity(intent);
                }
            });
        }
        public List<Product> init(){
            ArrayList<Product> pros = new ArrayList<Product>();
            Product product1 = new Product("活着",R.drawable.cxt1);
            Product product2 = new Product("童话",R.drawable.cxt2);
            Product product3 = new Product("古风",R.drawable.cxt3);
            Product product4 = new Product("名著",R.drawable.cxt4);
            pros.add(product1);
            pros.add(product2);
            pros.add(product3);
            pros.add(product4);
            return pros;

        }
    }
    class QgHolder extends RecyclerView.ViewHolder{
        List<Product> pro_list3;
        TextView djsTV;
        RecyclerView recyclerView3;
        private Long totalTime = new Date().getTime() + 2*60*60*1000;
        SimpleDateFormat simpleDateFormat;
        Handler handler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if (msg.what==9){
                    totalTime = totalTime - 1000;
                    Long nowTime = new Date().getTime();
                    Date date = new Date(totalTime-nowTime);
                    String str = simpleDateFormat.format(date);
                    djsTV.setText(str);
                    if (totalTime-nowTime>0){
                        handler.sendEmptyMessageDelayed(9,1000);
                    }else {
                        handler.removeMessages(9);
                    }
                }
            }
        };
        public QgHolder(@NonNull View itemView) {
            super(itemView);
            recyclerView3 = (RecyclerView)itemView.findViewById(R.id.qg_recycleView);
            pro_list3 = init();
            QgAdapter qgAdapter = new QgAdapter(context,pro_list3);
            recyclerView3.setAdapter(qgAdapter);
            djsTV = itemView.findViewById(R.id.kill_djs);
            simpleDateFormat = new SimpleDateFormat("hh:mm:ss");
//            GridLayoutManager gridLayoutManager = new GridLayoutManager(context,4);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
            recyclerView3.setLayoutManager(linearLayoutManager);
            setDate();
        }

        public List<Product> init(){
            ArrayList<Product> pros = new ArrayList<Product>();
            Product product1 = new Product("动物世界",R.drawable.qgt1);
            Product product2 = new Product("那么大",R.drawable.qgt2);
            Product product3 = new Product("湘行散记",R.drawable.qgt3);
            Product product4 = new Product("思维导图",R.drawable.qgt4);
            Product product5 = new Product("小坡的生日",R.drawable.qgt5);
            Product product6 = new Product("世界那么大",R.drawable.qgt6);
            Product product7 = new Product("森林报·春",R.drawable.qgt7);
            Product product8 = new Product("和爸妈去旅行",R.drawable.qgt8);
            Product product9 = new Product("写给儿子的38封信",R.drawable.qgt9);
            Product product10 = new Product("他来时有星光",R.drawable.qgt10);
            Product product11 = new Product("沉思录",R.drawable.qgt11);

            pros.add(product1);
            pros.add(product2);
            pros.add(product3);
            pros.add(product4); pros.add(product5); pros.add(product6); pros.add(product7); pros.add(product8); pros.add(product9); pros.add(product10); pros.add(product11);
            return pros;

        }
        public void setDate(){
            //发送延时时间。每隔一秒，减一并设置到主页面上
            handler.sendEmptyMessageDelayed(9,1000);
        }
    }
    class SgHolder extends RecyclerView.ViewHolder{
        RecyclerView recyclerView;
        List<Product3> product2List;

        public SgHolder(@NonNull View itemView) {
            super(itemView);
            product2List = setData();
            recyclerView = itemView.findViewById(R.id.kill_recycle);
            SgAdapter sgAdapter = new SgAdapter(context,product2List);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context,3);
            recyclerView.setLayoutManager(gridLayoutManager);
            recyclerView.setAdapter(sgAdapter);
            sgAdapter.setOnCommProClick(new SgAdapter.OnCommProClick() {
                @Override
                public void onItemClick(Product3 product) {
                    //跳转到商品详
                    Intent intent = new Intent(context, ProinfoActivity.class);
                    intent.putExtra("product",product);
                    context.startActivity(intent);
                }
            });
        }

        public List<Product3> setData(){
            ArrayList<Product3> products = new ArrayList<Product3>();
            Product3 product1 = new Product3(R.drawable.sga1,"儒林外史",22.2);
            Product3 product2 = new Product3(R.drawable.sga2,"人间失格",21);
            Product3 product3 = new Product3(R.drawable.sga3,"一个陌生女人的来信",20.2);
            Product3 product4 = new Product3(R.drawable.sga4,"你是人间四月天",22);
            Product3 product5 = new Product3(R.drawable.sga5,"中国通史",28.2);
            Product3 product6 = new Product3(R.drawable.sga6,"泰戈尔诗集",29);
            Product3 product7 = new Product3(R.drawable.sga7,"中国近代史",12);
            Product3 product8 = new Product3(R.drawable.sga8,"克罗夫预言",22);
            Product3 product9 = new Product3(R.drawable.sga9,"小鹿斑比",10);
            products.add(product1);products.add(product2);products.add(product3);
            products.add(product4);products.add(product5);products.add(product6);
            products.add(product7);products.add(product8);products.add(product9);
            return products;
        }
    }
    class TjHolder extends RecyclerView.ViewHolder{
        RecyclerView recyclerView;
        List<Product4> product3List;
        public TjHolder(@NonNull View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.tj_recycle);
            product3List = setData();
            TjAdapter tjAdapter = new TjAdapter(context,product3List);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context,3);
            recyclerView.setLayoutManager(gridLayoutManager);
            recyclerView.setAdapter(tjAdapter);
            tjAdapter.setOnCommProClick(new TjAdapter.OnCommProClick() {
                @Override
                public void onItemClick(Product4 product) {
                    //跳转到商品详
                    Intent intent = new Intent(context, ProActivity.class);
                    intent.putExtra("product",product);
                    context.startActivity(intent);
                }
            });
        }

        public List<Product4> setData(){
            ArrayList<Product4> products = new ArrayList<Product4>();
            Product4 product1 = new Product4(R.drawable.tja1,71.1,890,"暗夜与黎明");
            Product4 product2 = new Product4(R.drawable.tja2,49,21,"资治通鉴");
            Product4 product3 = new Product4(R.drawable.tja3,49,720,"西兰花先生");
            Product4 product4 = new Product4(R.drawable.tja4,59,120,"亲亲自然");
            Product4 product5 = new Product4(R.drawable.tja5,68.89,280,"传世经典");
            Product4 product6 = new Product4(R.drawable.tja6,89.9,290,"半小时");
            Product4 product7 = new Product4(R.drawable.tja7,183.90,120,"你好！数学");
            Product4 product8 = new Product4(R.drawable.tja8,57.82,220,"深见春天");
            Product4 product9 = new Product4(R.drawable.tja9,129,1000,"中国历史");
            products.add(product1);products.add(product2);products.add(product3);
            products.add(product4);products.add(product5);products.add(product6);
            products.add(product7);products.add(product8);products.add(product9);
            return products;
        }

    }




    class RxHolder extends RecyclerView.ViewHolder{
        RecyclerView recyclerView;
        List<Product7> product2List;
        public RxHolder(@NonNull View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.rx_recycle);
            product2List = setData();

            rxAdapter = new RxAdapter(context,product2List);
            StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(staggeredGridLayoutManager);
            recyclerView.setAdapter(rxAdapter);
            //设置监听事件
            rxAdapter.setOnCommProClick(new RxAdapter.OnCommProClick() {
                @Override
                public void onItemClick(Product7 product) {
                    //跳转到商品详
                    Intent intent = new Intent(context, ProRxActivity.class);
                    intent.putExtra("product",product);
                    context.startActivity(intent);
                }
            });
        }
        public List<Product7> setData(){
            ArrayList<Product7> products = new ArrayList<Product7>();
            Product7 product1 = new Product7(R.drawable.tja1,"暗夜与黎明",890);
            Product7 product2 = new Product7(R.drawable.tja2,"资治通鉴",21);
            Product7 product3 = new Product7(R.drawable.tja3,"西兰花先生",720);
            Product7 product4 = new Product7(R.drawable.tja4,"亲亲自然",120);
            Product7 product5 = new Product7(R.drawable.tja5,"传世经典",280);
            Product7 product6 = new Product7(R.drawable.tja6,"半小时",290);
            Product7 product7 = new Product7(R.drawable.tja7,"你好！数学",120);
            Product7 product8 = new Product7(R.drawable.tja8,"深见春天",220);
            Product7 product9 = new Product7(R.drawable.tja9,"中国历史",1000);
            Product7 product10 = new Product7(R.drawable.sga1,"儒林外史",22.2);
            Product7 product11 = new Product7(R.drawable.sga2,"人间失格",21);
            Product7 product12 = new Product7(R.drawable.sga3,"一个陌生女人的来信",20.2);
            Product7 product13 = new Product7(R.drawable.sga4,"你是人间四月天",22);
            Product7 product14 = new Product7(R.drawable.sga5,"中国通史",28.2);
            Product7 product15 = new Product7(R.drawable.sga6,"泰戈尔诗集",29);
            Product7 product16 = new Product7(R.drawable.sga7,"中国近代史",12);
            Product7 product17 = new Product7(R.drawable.sga8,"克罗夫预言",22);
            Product7 product18 = new Product7(R.drawable.sga9,"小鹿斑比",10);
            products.add(product1);products.add(product2);products.add(product3);
            products.add(product4);products.add(product5);products.add(product6);
            products.add(product7);products.add(product8);products.add(product9);
            products.add(product10);products.add(product13);products.add(product16);
            products.add(product11);products.add(product14);products.add(product17);
            products.add(product12);products.add(product15);products.add(product18);
            return products;

        }
    }
    class AdHolder extends RecyclerView.ViewHolder{
        public AdHolder(@NonNull View itemView) {
            super(itemView);
            //创建三个试图文件放入pageAdapter中，在注入到viewpage中
            initData(itemView);
        }
        public void initData(View itemView){
            //创建3个view


            ImageView imageView1 = new ImageView(context);
            imageView1.setImageResource(R.drawable.c1);
            imageView1.setScaleType(ImageView.ScaleType.FIT_XY);


            ImageView imageView2 = new ImageView(context);
            imageView2.setImageResource(R.drawable.c2);
            imageView2.setScaleType(ImageView.ScaleType.FIT_XY);

            ImageView imageView3 = new ImageView(context);
            imageView3.setImageResource(R.drawable.c3);
            imageView3.setScaleType(ImageView.ScaleType.FIT_XY);

            List<View> views = new ArrayList<View>();
            views.add(imageView1);
            views.add(imageView2);
            views.add(imageView3);

            //放入到pageAdaoter中
            AdAdapter adAdapter = new AdAdapter(views);
            //向viewPager中设置adapter来显示
            ViewPager viewPager = itemView.findViewById(R.id.vp1);
            viewPager.setAdapter(adAdapter);
            //美化操作
            viewPager.setPageMargin(20);
            viewPager.setOffscreenPageLimit(3);
            viewPager.setPageTransformer(true, new RotateDownPageTransformer());
        }
    }

    //有三个板块，需要联网请求
    //不同的板块，需要不同的数据
    //什么时候需要请求数据1、进入页面时需要数据2、下接刷新时3、触底时需要热门板块的数据

}
